/*
 * Copyright (c) 2009-2010, Petr Panteleyev
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 *    1. Redistributions of source code must retain the above copyright notice,
 *       this list of conditions and the following disclaimer.
 *    2. Redistributions in binary form must reproduce the above copyright notice,
 *       this list of conditions and the following disclaimer in the documentation
 *       and/or other materials provided with the distribution.
 *    3. The name of the author may not be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */


package org.panteleyev.blogclient.options;

import java.awt.Font;
import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import org.panteleyev.blogclient.FontManager;
import org.panteleyev.swing.FontChooser;
import org.panteleyev.swing.SwingFactory;
import org.panteleyev.swing.WindowManager;
import org.panteleyev.swing.events.FontsUpdatedEvent;

/**
 *
 * @author Petr Panteleyev <petr@panteleyev.org>
 */
class FontsPanel extends OptionsDialogPanel {
    private JTextField editorFontLabel;
    private FontChooser<?> fontChooser;
    private JTextField ljAdminConsoleFontLabel;

    /** Creates new form FlickrPanel */
    public FontsPanel() {
        initComponents();
    }

    private void initComponents() {
        setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));

        JLabel l1, l2;
        JPanel p1, p2;

        SwingFactory.add(this,
            p1 = SwingFactory.createPanel(BorderFactory.createEmptyBorder(5, 5, 5, 5), SwingFactory.LAYOUT.BOX_X,
                l1 = new JLabel(L10N.OPTIONS_DIALOG_FONTS_PANEL_EDITOR.s()),
                Box.createHorizontalStrut(3),
                editorFontLabel = new JTextField(),
                Box.createHorizontalGlue(),
                new JButton(new AbstractAction("...") {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        fontChooser = new FontChooser<JFrame>(null);
                        fontChooser.setFontData(FontManager.getEditorFont());
                        if (fontChooser.showDialog()) {
                            Font font = fontChooser.getSelectedFont();
                            editorFontLabel.setFont(font);
                            editorFontLabel.setText(buildFontDescription(font));
                            FontManager.setEditorFont(font);
                            WindowManager.sendApplicationEvent(new FontsUpdatedEvent(this));
                        }
                    }
                })
            ),
            p2 = SwingFactory.createPanel(BorderFactory.createEmptyBorder(5, 5, 5, 5), SwingFactory.LAYOUT.BOX_X,
                l2 = new JLabel("LJ Admin Console:"),
                Box.createHorizontalStrut(3),
                ljAdminConsoleFontLabel = new JTextField(),
                Box.createHorizontalGlue(),
                new JButton(new AbstractAction("...") {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        fontChooser = new FontChooser<JFrame>(null);
                        fontChooser.setFontData(FontManager.getLJAdminConsoleFont());
                        if (fontChooser.showDialog()) {
                            Font font = fontChooser.getSelectedFont();
                            ljAdminConsoleFontLabel.setFont(font);
                            ljAdminConsoleFontLabel.setText(buildFontDescription(font));
                            FontManager.setLJAdminConsoleFont(font);
                            WindowManager.sendApplicationEvent(new FontsUpdatedEvent(this));
                        }
                    }
                })
            ),
            Box.createVerticalGlue()
        );

        SwingFactory.setCommonPreferredSize(l1, l2);

        editorFontLabel.setEditable(false);
        if (FontManager.getEditorFont() != null) {
            editorFontLabel.setFont(FontManager.getEditorFont());
        }
        ljAdminConsoleFontLabel.setEditable(false);
        if (FontManager.getLJAdminConsoleFont() != null) {
            ljAdminConsoleFontLabel.setFont(FontManager.getLJAdminConsoleFont());
        }

        SwingFactory.setHeightMaximumToPreferred(p1, p2);
    }

    private String buildFontDescription(Font font) {
        StringBuilder b = new StringBuilder(font.getFamily());
        b.append(" ");
        b.append(Integer.toString(font.getSize()));

        return b.toString();
    }

    @Override
    void onShow() {
        Font font = FontManager.getEditorFont();
        if (font != null) {
            editorFontLabel.setText(font.getFamily());
            editorFontLabel.setText(buildFontDescription(font));
        } else {
            editorFontLabel.setText("Default Font");
        }

        font = FontManager.getLJAdminConsoleFont();
        if (font != null) {
            ljAdminConsoleFontLabel.setText(font.getFamily());
            ljAdminConsoleFontLabel.setText(buildFontDescription(font));
        } else {
            ljAdminConsoleFontLabel.setText("Default Font");
        }
    }

    @Override
    void onOk() {
    }

}
